package pl.vojna.ant_alg;

import java.io.File;
import java.util.Random;

import pl.vojna.ant_alg.algorithms.AntSystemLocalSearch;
import pl.vojna.ant_alg.algorithms.RandomSearch;
import pl.vojna.ant_alg.lokal_search_alg.TabuSearch;
import pl.vojna.ant_alg.tests.PermutationGenerator;

public class Main {

    private static Random r = new Random();

    private static String randomRow(int size, int limit) {
        int random;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            random = r.nextInt(limit);
            sb.append(random);
            sb.append("\n");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int popSize = 20;

        double pheromoneFactor = 2;
        double heuristicFactor = 2;
        double pheromoneVanish = 0.2;
        double pheromoneStrength = 10000;
        String fileName = "./data/chr12c.dat";
        /*
         * StringBuilder problemDesc = new StringBuilder();
         * problemDesc.append(problemSize); problemDesc.append("\n"); for (int i
         * = 0; i < 2 * problemSize; i++) {
         * problemDesc.append(randomRow(problemSize, 100)); } Problem problem =
         * new Problem(problemDesc.toString()); System.out.println(problem);
         * 
         * 
         * problem.wirteToFile(fileName);
         */
        Problem problem = new Problem(new File(fileName));
        System.out.println(problem);
        AntSystemLocalSearch antSystem = new AntSystemLocalSearch(
                heuristicFactor, pheromoneFactor, pheromoneVanish,
                pheromoneStrength);
        int best[] = antSystem.solve(problem, popSize, Boolean.FALSE, 5);

        System.out.println("BEST: ");
        Utils.showSolution(best);
        System.out.println(Utils.solutionCost(problem, best));
        // System.out.println("BEST AFTER TABU: ");
        TabuSearch tabu = new TabuSearch();
        int tabuIterations = 100;
        /*
         * int bestOpt[] = tabu.improve(best, problem, tabuIterations);
         * Utils.showSolution(bestOpt);
         * System.out.println(Utils.solutionCost(problem, bestOpt));
         */

        int tabuFromId[] = Utils.newIdPermutation(problem.getSize());
        System.out.println("TABU OPT FROM ID: ");
        int tabuOpt[] = tabu.improve(tabuFromId, problem, tabuIterations);
        Utils.showSolution(tabuOpt);
        System.out.println(Utils.solutionCost(problem, tabuOpt));

        RandomSearch rs = new RandomSearch();
        int randomIter = 100000;
        int randomBest[] = rs.solve(problem, 0, Boolean.FALSE, randomIter);
        System.out.println("RANDOM BEST: ");
        Utils.showSolution(randomBest);
        System.out.println("COST: " + Utils.solutionCost(problem, randomBest));

        /*
         * int optimal[] = computeOptimal(problem);
         * System.out.println("OPTIMAL: "); Utils.showSolution(optimal);
         * System.out.println(Utils.solutionCost(problem, optimal));
         */

    }

    private static int[] computeOptimal(Problem problem) {
        PermutationGenerator pg = new PermutationGenerator(problem.getSize());

        int best[] = pg.getNext();
        int pretender[];
        double pretenderCost;
        double bestCost = Utils.solutionCost(problem, best);
        while (pg.hasMore()) {
            pretender = pg.getNext();
            pretenderCost = Utils.solutionCost(problem, pretender);
            if (pretenderCost < bestCost) {
                best = pretender;
                bestCost = pretenderCost;
                System.out.println(bestCost);
                Utils.showSolution(best);
                if (!pg.hasMore()) {
                    return pretender;
                }
            }

        }
        Utils.showSolution(best);
        return best;

    }
}
